package cn.itcast.dao.impl;

import cn.itcast.dao.refactor.AbstractDao;
import cn.itcast.po.User;
import java.sql.ResultSet;
import java.sql.SQLException;


/*
* (模板设计模式)
* 继承优化JDBC缺陷: ResultSet返回值映射到User时的冗余
*   sql查询时，根据返回值的不同，父类需要定义不同的rowMapping函数。
* */
public class UserDaoImpl extends AbstractDao {

    public void updateUser(User user) {
        String sql = "UPDATE user SET address=? WHERE id=?";
        Object[] obj = new Object[]{user.getAddress(), user.getId()};
        super.updateUser(sql, obj);
    }

    public User findUser(int id){
        String sql = "select * from user where id=?";
        return (User)super.findUser(sql, id);
    }

    @Override
    protected Object rowMapping(ResultSet rs) throws SQLException {
        User user = new User();

        user.setName(rs.getString("username"));
        user.setDate(rs.getDate("birthday"));
        user.setSex(rs.getString("sex"));
        user.setAddress(rs.getString("address"));
        return user;
    }
}
